package org.mch.func.file.findcopy;

import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFileChooser;

import org.apache.commons.io.FileUtils;
import org.mch.biz.ui.BizEvent;
import org.mch.biz.ui.BizPanel;
import org.mch.main.MCHFrame;
import org.mch.swingm.core.MFileChooser;

/**
 * 描述：文件操作；
 * 
 * @author MCHUN；
 * 
 */
public class ClientEvent extends BizEvent {
	private static final long serialVersionUID = -8590108060646853821L;

	MFileChooser fileChooser;

	public ClientEvent(BizPanel panel) {
		super(panel);
	}

	protected void buttonClicked(MouseEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_browse_find()) {
			onBrowse_findpath();
		}
		if (e.getSource() == getPanel().getBtn_browse_copy()) {
			onBrowse_copypath();
		}
		if (e.getSource() == getPanel().getBtn_execute()) {
			execute();
		}
	}

	protected void onBrowse_findpath() {
		int tmp = getFileChooser().showOpenDialog(MCHFrame.getFrame());
		if (tmp == JFileChooser.APPROVE_OPTION) {
			getPanel().getText_find_path().setText(getFileChooser().getSelectedFile().getPath());
		}
	}

	protected void onBrowse_copypath() {
		int tmp = getFileChooser().showOpenDialog(MCHFrame.getFrame());
		if (tmp == JFileChooser.APPROVE_OPTION) {
			getPanel().getText_copy_path().setText(getFileChooser().getSelectedFile().getPath());
		}
	}

	private Set<String> fileNameSet = new HashSet<String>();
	private String find_path = null;
	private String copy_path = null;

	public void execute() {
		fileNameSet.clear();
		find_path = null;
		copy_path = null;

		String s = getPanel().getText_filename().getText();
		String[] filenames = s.split("\n");
		for (String fname : filenames) {
			fileNameSet.add(fname);
		}

		find_path = getPanel().getText_find_path().getText();
		copy_path = getPanel().getText_copy_path().getText();

		File findFile = new File(find_path);
		File[] files_1 = findFile.listFiles();

		findAllFileNames(files_1);// 循环所有文件，一个一个比较
	}

	protected void findAllFileNames(File[] files) {

		for (int i = 0, len = files.length; i < len; i++) {
			File tmpfile = files[i];
			if (tmpfile.isDirectory()) {
				findAllFileNames(tmpfile.listFiles());
			} else if (tmpfile.isFile()) {
				String tmpname = tmpfile.getName();
				if (fileNameSet.contains(tmpname)) {
					// 如果是需要查询的文件，则进行复制
					try {
						FileUtils.copyFile(tmpfile, new File(copy_path + File.separator + tmpname));
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

	}

	public MFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new MFileChooser();
			fileChooser.setPreferredSize(700, 400);
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// 设置能选择文件和文件夹
		}
		return fileChooser;
	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}